#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <api/libpub/lib_pub.h>
#include <api/libpub/lib_as.h>
#include <hld/nim/nim.h>
#include <hld/decv/decv.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"

#include "win_com_menu_define.h"
//#include "win_auto_scan.h"

/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER        g_win_autoscan;
extern CONTAINER        item_country;     //country
extern CONTAINER        item_fta_only;    // FTA Only
extern CONTAINER        item_start;       //start search
extern TEXT_FIELD       item_country_label;
extern TEXT_FIELD       item_fta_only_label;
extern TEXT_FIELD       item_start_label;
extern MULTISEL         item_country_sel;
extern MULTISEL         item_fta_only_sel;

#ifdef DVBT2_SUPPORT
extern CONTAINER        item_t2_signal_only;    // T2 Only
extern TEXT_FIELD       item_t2_signal_only_label;
extern MULTISEL         item_t2_signal_only_sel;
#ifdef DVBT2_LITE_SUPPORT
extern CONTAINER        item_t2_lite_support;    // T2-Lite support
extern TEXT_FIELD       item_t2_lite_support_label;
extern MULTISEL         item_t2_lite_support_sel;
#endif
#endif

#ifdef POLAND_SPEC_SUPPORT
extern CONTAINER		item_action; //action	
extern TEXT_FIELD		item_action_label; //action	
extern MULTISEL		action_sel;
#endif

#if 1//ChengYu@test
#include "time_zone_name.h"
/*Martin@20150129*/
typedef struct
{
	UINT16 region_str;
	UINT8 timezone;
	UINT8 country_code[3];// reference to ISO3166-1
}REGION_PARAM_T;
//----------
extern REGION_PARAM_T region_param[];
#endif

static VACTION autoscan_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT autoscan_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static VACTION autoscan_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT autoscan_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static VACTION autoscan_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT autoscan_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define TXT_BUTTON_IDX	 WSTL_BUTTON_02_HD//WSTL_NL_BUTTON_01_HD
/****************************************************************************************/
#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    autoscan_item_keymap,autoscan_item_callback,  \
    conobj, ID,1)

#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)


#define LDEF_TXT_BUTTON(root,varTxt,nxtObj,ID,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,0,0,0,0, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_MSEL(root, varNum, nxtObj, ID, l, t, w, h,style,cur,cnt,ptabl)	\
    DEF_MULTISEL(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, TXTS_SH_IDX,TXTS_HL_IDX,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    autoscan_item_sel_keymap,autoscan_item_sel_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 20,0,style,ptabl,cur,cnt)


#define LDEF_MENU_ITEM_SEL(root,varCon,nxtObj,varTxt,varNum,ID,IDu,IDd,l,t,w,h,resID,style,cur,cnt,ptbl) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXTN_L_OF,t + TXTN_T_OF,TXTN_W,TXTN_H,resID)    \
    LDEF_MSEL(&varCon,varNum,NULL,ID, l + TXTS_L_OF ,t + TXTS_T_OF,TXTS_W,TXTS_H,style,cur,cnt,ptbl)

/*
#define LDEF_MENU_ITEM_TXT(root,varCon,nxtObj,varTxt,ID,IDu,IDd,l,t,w,h,resID) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT_BUTTON(&varCon,varTxt,NULL,ID,l + TXTN_L_OF,t + TXTN_T_OF,TXTN_W-50,TXTN_H,resID)
*/    
#define LDEF_MENU_ITEM_TXT(root,varCon,nxtObj,varTxt,ID,IDu,IDd,l,t,w,h,resID) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT_BUTTON(&varCon,varTxt,NULL,ID,l + TXTN_L_OF,t + TXTN_T_OF,TXTN_W-50,TXTN_H,resID)


#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    autoscan_keymap,autoscan_callback,  \
    nxtObj, focusID,0)

#define WIN g_win_autoscan

enum
{
	COUNTRY_ID=1,
	FTA_ONLY_ID,
	#ifdef	DVBT2_SUPPORT 
	T2_ONY_ID,
		#ifdef DVBT2_LITE_SUPPORT		
	T2_LITE_SUPPORT_ID,
		#endif
	#endif	

#ifdef POLAND_SPEC_SUPPORT
	POLAND_SPEC_ID,
#endif
	ANT_POWER_ID,
	START_ID,
};


POBJECT_HEAD autoscan_items[] =
{
	(POBJECT_HEAD)&item_country,
	(POBJECT_HEAD)&item_fta_only,
#ifdef DVBT2_SUPPORT
	(POBJECT_HEAD)&item_t2_signal_only,
	#ifdef DVBT2_LITE_SUPPORT
	(POBJECT_HEAD)&item_t2_lite_support,
	#endif
#endif	
	(POBJECT_HEAD)&item_start,
#ifdef POLAND_SPEC_SUPPORT
	(POBJECT_HEAD)&item_action,
#endif
};

extern UINT16 region_ids[];

//extern UINT16 yesno_ids[];

static UINT16 yesno_ids[] =
{
	RS_COMMON_NO,
	RS_COMMON_YES,
};

#ifdef POLAND_SPEC_SUPPORT
static UINT16 action_id[]=
{
	RS_REINSTALL,
	RS_UPDATE,
};
#define ACTION                         (action_sel.nSel)
#endif

/*******************************************************************************
*	Component  Object  Define
*******************************************************************************/
#ifdef POLAND_SPEC_SUPPORT
	#ifdef DVBT2_SUPPORT
	LDEF_MENU_ITEM_SEL(WIN, item_country, &item_fta_only, item_country_label, item_country_sel, COUNTRY_ID, START_ID, FTA_ONLY_ID, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_SYSTEM_COUNTRY, STRING_ID, 0, 2, region_ids)
	LDEF_MENU_ITEM_SEL(WIN, item_fta_only, &item_t2_signal_only, item_fta_only_label, item_fta_only_sel, FTA_ONLY_ID, COUNTRY_ID, T2_ONY_ID, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_FTA_ONLY, STRING_ID, 0, 2, yesno_ids)
		#ifdef DVBT2_LITE_SUPPORT
		LDEF_MENU_ITEM_SEL(WIN, item_t2_signal_only, &item_t2_lite_support, item_t2_signal_only_label, item_t2_signal_only_sel, T2_ONY_ID, FTA_ONLY_ID, T2_LITE_SUPPORT_ID, \
		 CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, 0, STRING_ID, 0, 2, yesno_ids)
		LDEF_MENU_ITEM_SEL(WIN, item_t2_lite_support, &item_action, item_t2_lite_support_label, item_t2_lite_support_sel, T2_LITE_SUPPORT_ID, T2_ONY_ID, POLAND_SPEC_ID, \
		 CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, 0, STRING_ID, 0, 2, yesno_ids)
		LDEF_MENU_ITEM_SEL(WIN, item_action, &item_start, item_action_label, action_sel, POLAND_SPEC_ID, T2_LITE_SUPPORT_ID, START_ID, \
		                   CON_L, CON_T + (CON_H + CON_GAP)*4 CON_W, CON_H, RS_ACTION, STRING_ID, 0, 2, action_id)
		LDEF_MENU_ITEM_TXT(WIN, item_start, NULL, item_start_label, START_ID, POLAND_SPEC_ID, COUNTRY_ID, \
		                   CON_L, CON_T + (CON_H + CON_GAP)*5, CON_W, CON_H, RS_COMMON_SEARCH)
		#else
		LDEF_MENU_ITEM_SEL(WIN, item_t2_signal_only, &item_action, item_t2_signal_only_label, item_t2_signal_only_sel, FTA_ONLY_ID, COUNTRY_ID, START_ID, \
	                 CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, 0, STRING_ID, 0, 2, yesno_ids)
		LDEF_MENU_ITEM_SEL(WIN, item_action, &item_start, item_action_label, action_sel, POLAND_SPEC_ID, T2_LITE_SUPPORT_ID, START_ID, \
		                   CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, RS_ACTION, STRING_ID, 0, 2, action_id)
		LDEF_MENU_ITEM_TXT(WIN, item_start, NULL, item_start_label, START_ID, POLAND_SPEC_ID, COUNTRY_ID, \
		                   CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_COMMON_SEARCH)
		#endif
	#else
	LDEF_MENU_ITEM_SEL(WIN, item_country, &item_fta_only, item_country_label, item_country_sel, 1, 4, 2, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_SYSTEM_COUNTRY, STRING_ID, 0, 2, region_ids)
	LDEF_MENU_ITEM_SEL(WIN, item_fta_only, &item_action, item_fta_only_label, item_fta_only_sel, 2, 1, 3, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_FTA_ONLY, STRING_ID, 0, 2, yesno_ids)
	LDEF_MENU_ITEM_SEL(WIN, item_action, &item_start, item_action_label, action_sel, 3, 2, 4, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_ACTION, STRING_ID, 0, 2, action_id)
	LDEF_MENU_ITEM_TXT(WIN, item_start, NULL, item_start_label, 4, 3, 1, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, RS_COMMON_SEARCH)
	#endif		                   
#else
	LDEF_MENU_ITEM_SEL(WIN, item_country, &item_fta_only, item_country_label, item_country_sel, COUNTRY_ID, START_ID, FTA_ONLY_ID, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_SYSTEM_COUNTRY, STRING_ID, 0, 2, region_ids)
	#ifdef DVBT2_SUPPORT
	LDEF_MENU_ITEM_SEL(WIN, item_fta_only, &item_t2_signal_only, item_fta_only_label, item_fta_only_sel, FTA_ONLY_ID, COUNTRY_ID, T2_ONY_ID, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_FTA_ONLY, STRING_ID, 0, 2, yesno_ids)
		#ifdef DVBT2_LITE_SUPPORT
		LDEF_MENU_ITEM_SEL(WIN, item_t2_signal_only, &item_t2_lite_support, item_t2_signal_only_label, item_t2_signal_only_sel, T2_ONY_ID, FTA_ONLY_ID, T2_LITE_SUPPORT_ID, \
		                   CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, 0, STRING_ID, 0, 2, yesno_ids)
		LDEF_MENU_ITEM_SEL(WIN, item_t2_lite_support, &item_start, item_t2_lite_support_label, item_t2_lite_support_sel, T2_LITE_SUPPORT_ID, T2_ONY_ID, START_ID, \
		                   CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, 0, STRING_ID, 0, 2, yesno_ids)
		LDEF_MENU_ITEM_TXT(WIN, item_start, NULL, item_start_label, START_ID, T2_LITE_SUPPORT_ID, COUNTRY_ID, \
		                 CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_COMMON_SEARCH)
		#else
		LDEF_MENU_ITEM_SEL(WIN, item_t2_signal_only, &item_start, item_t2_signal_only_label, item_t2_signal_only_sel, T2_ONY_ID, FTA_ONLY_ID, START_ID, \
	                 CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_FTA_ONLY, STRING_ID, 0, 2, yesno_ids)
		LDEF_MENU_ITEM_TXT(WIN, item_start, NULL, item_start_label, START_ID, T2_ONY_ID, COUNTRY_ID, \
		                 CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, RS_COMMON_SEARCH)
		#endif	 
	#else
	LDEF_MENU_ITEM_SEL(WIN, item_fta_only, &item_start, item_fta_only_label, item_fta_only_sel, FTA_ONLY_ID, COUNTRY_ID, START_ID, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_FTA_ONLY, STRING_ID, 0, 2, yesno_ids)
	LDEF_MENU_ITEM_TXT(WIN, item_start, NULL, item_start_label, START_ID, FTA_ONLY_ID, COUNTRY_ID, \
	                   CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_COMMON_SEARCH)
	#endif
#endif
LDEF_WIN(WIN, &item_country, W_L, W_T, W_W, W_H, START_ID)


static BOOL current_frontend_is_isdbt = FALSE;
//modify for channel scan mode show the title auto scan on T tuner 2011 11 25
extern BOOL is_auto_scan;

//#define AS_FREQ_MIN     950
//#define AS_FREQ_MAX     2200
void win_autoscan_set_search_param(void)
{
	UINT32  search_mode;
	UINT32 prog_type;
	S_NODE s_node;
	struct as_service_param param;

	struct vdec_device *vdec;
	struct vdec_io_get_frm_para vfrm_param;
	UINT32 addr, len;
	UINT8 uBandCnt,index_BandCnt;
	SYSTEM_DATA* sys_data;
	sys_data = sys_data_get();
#ifdef POLAND_SPEC_SUPPORT
       extern g_enter_welcom;
       TIMER_SET_CONTENT* timer;
  
       INT16 i,n;
     
	if(g_enter_welcom == 3)
	{
		action_sel.nSel = 1; //update
	}
	
	if(ACTION == 0) //reinstall
	{
		sim_close_monitor(0);
		n = get_sat_num(VIEW_ALL);
		recreate_prog_view(PROG_ALL_MODE,0);
		for(i=0;i<n;i++)
		{
		get_sat_at(i, VIEW_ALL,&s_node);
		del_child_prog(TYPE_SAT_NODE, s_node.sat_id);
		}
		
		update_data();
		sys_data_check_channel_groups();	
                    /*turn off all timer*/
             
                    for(i=0;i<MAX_TIMER_NUM;i++)
                    {
	                    timer = &sys_data->timer_set.TimerContent[i];
	                    timer->timer_mode = TIMER_MODE_OFF;                                    
                    }
					
		SetAutoScanUpdateMode(FALSE);			
	}
	else
	{
		SetAutoScanUpdateMode(TRUE);
	}
#endif

	//tv/radio/all
	prog_type = P_SEARCH_TV|P_SEARCH_RADIO;
	
	/* FTA Only */
	if (OSD_GetMultiselSel(&item_fta_only_sel) == 1)
		search_mode = P_SEARCH_FTA|P_SEARCH_NIT;
	else// if (sSATSRCH_FTA_IDX == 1)
		search_mode = P_SEARCH_FTA|P_SEARCH_SCRAMBLED|P_SEARCH_NIT;
    
	//get_default_bandparam(OSD_GetMultiselSel(&item_country_sel),(Band_param*)  &uCountryParam);	
	  OSD_SetMultiselSel(&item_country_sel, sys_data->country);

    if(current_frontend_is_isdbt)
        get_isdbt_bandparam(OSD_GetMultiselSel(&item_country_sel), (Band_param *)&uCountryParam);
    else
        get_dvbt_bandparam(OSD_GetMultiselSel(&item_country_sel), (Band_param *)&uCountryParam);

#ifdef POLAND_SPEC_SUPPORT 
    if(region_ids[sys_data->country]==RS_REGION_POL)
        get_dvbt_bandparam_for_poland((Band_param *)&uCountryParam);
#endif

	uBandCnt = 0;
	for(index_BandCnt=0;index_BandCnt<MAX_BAND_COUNT;index_BandCnt++)
	{
		if(uCountryParam[index_BandCnt].start_freq==0)//warning shooting xing
			break;
		else
			uBandCnt++;		
	}

    if(current_frontend_is_isdbt)
        param.sat_ids[0] = get_frontend_satid(FRONTEND_TYPE_T, 1);
    else
        param.sat_ids[0] = get_frontend_satid(FRONTEND_TYPE_T, 0);
	//param.as_from = AS_FREQ_MIN;
	//param.as_to = AS_FREQ_MAX;
	param.as_prog_attr = prog_type|search_mode;
	param.as_frontend_type = FRONTEND_TYPE_T; 	
	param.as_sat_cnt = 1;
	param.as_method = AS_METHOD_FFT;
	param.as_p_add_cfg = PROG_ADD_REPLACE_OLD;
	param.as_handler = NULL;
	param.band_group_param=uCountryParam;
	param.scan_cnt = uBandCnt;
    

	// set param 
	win_search_set_param(&param);

	//update_data();

	addr = __MM_AUTOSCAN_DB_BUFFER_ADDR;
	addr &= 0x8fffffff;
	len = __MM_AUTOSCAN_DB_BUFFER_LEN;

	db_search_init((UINT8 *)addr, len);
}


#define VACT_POP_UP	(VACT_PASS + 1)
static VACTION autoscan_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
  	case V_KEY_LEFT:
		act = VACT_DECREASE;
		break;
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;
	case V_KEY_ENTER:
		act = VACT_POP_UP;
		break;
	default:
		act = VACT_PASS;
	}
	return act;	        
}

static PRESULT autoscan_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	UINT32 sel;
	UINT8 bID;
	UINT16 * Uni_str;
	OSD_RECT rect;

	bID = OSD_GetObjID(pObj);

	switch(event)
	{
	case EVN_PRE_CHANGE:
		sel = *((UINT32*)param1);
		break;
	case EVN_POST_CHANGE:
		sel = param1;
		break;
	case EVN_REQUEST_STRING:
		sel = param1;
		Uni_str= (UINT16*)param2;
		break;
	case EVN_UNKNOWN_ACTION:
     /*
		unact = (VACTION)(param1>>16);		
		if(unact ==  VACT_POP_UP)
		{
			OSD_SetRect2(&rect,&pObj->frame);
			OSD_TrackObject((POBJECT_HEAD)&autoscan_item1, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
		}
		*/
		break;
     default:
        break;
	}
	return ret;    
}


static VACTION autoscan_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch (key)
	{
		case V_KEY_ENTER:
		//case V_KEY_RIGHT:
			act = VACT_ENTER;
			break;
		default:
			act = VACT_PASS;
	}

	return act;

}

static PRESULT autoscan_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 bID;
	VACTION unact;
	POBJECT_HEAD submenu;
	UINT8 back_saved;
	SYSTEM_DATA* sys_data = sys_data_get();	
	bID = OSD_GetObjID(pObj);


	UINT32 searchT2SignalOnly;
	struct nim_device *nim_dev;
  	if(g_tuner_num == 1)
		cur_tuner_idx = 0;
	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	

	switch(event)
	{
		case EVN_UNKNOWN_ACTION:
			unact = (VACTION)(param1>>16);
			if(unact == VACT_ENTER && bID == START_ID)
			{
				#ifdef DVBT2_SUPPORT
				if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev, HLD_DEV_TYPE_NIM))
				{
					searchT2SignalOnly = OSD_GetMultiselSel(&item_t2_signal_only_sel);
					nim_io_control(nim_dev, NIM_DRIVER_SEARCH_T2_SIGNAL_ONLY, searchT2SignalOnly);
					#ifdef DVBT2_LITE_SUPPORT
	 					sys_data->t2_lite_support= OSD_GetMultiselSel(&item_t2_lite_support_sel);
						nim_io_control(nim_dev, NIM_DRIVER_T2_LITE_ENABLE, (UINT32)(sys_data->t2_lite_support));
	 				#endif
				}
				#endif
				sys_data->country = OSD_GetMultiselSel(&item_country_sel);
				sys_data_save(1);          
				win_autoscan_set_search_param();
				submenu = (POBJECT_HEAD)&g_win_search;
				if(OSD_ObjOpen(submenu, 0xFFFFFFFF) != PROC_LEAVE)
					menu_stack_push(submenu);				
				ret = PROC_LOOP;
			}
			break;
	}
	return ret;    
}


static VACTION autoscan_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
  	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:
		act = VACT_PASS;
	}
	return act;	    
}

static PRESULT autoscan_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	MULTISEL* msel;
	TEXT_FIELD *pTxt;
	UINT32 searchT2SignalOnly;

	UINT16 title_id,sat_idx,sat_cnt;
	OSD_RECT rect;
	POBJECT_HEAD lastitem;
	SYSTEM_DATA* sys_data = sys_data_get();
	UINT16 i;
	UINT8 flag;
	S_NODE s_node;


	struct nim_device *nim_dev;
  	if(g_tuner_num == 1)
		cur_tuner_idx = 0;
	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	
	
	switch(event)
	{
	case EVN_PRE_OPEN:
        if(param2 != MENU_OPEN_TYPE_STACK)
        {
            if((param2 & MENU_FOR_ISDBT) == MENU_FOR_ISDBT)
                current_frontend_is_isdbt = TRUE;           
            else
                current_frontend_is_isdbt = FALSE;
        }
        if(current_frontend_is_isdbt)
            title_id = RS_ISDBT_AUTO_SCAN;
        else            
            title_id = RS_DVBT_AUTO_SCAN;

        OSD_SetMultiselCount(&item_country_sel, get_region_cnt());
        OSD_SetMultiselSelTable(&item_country_sel, region_ids); 
        //OSD_SetMultiselSel(&item_country_sel, sys_data->region_id);
        OSD_SetMultiselSel(&item_country_sel, sys_data->country);

	#ifdef DVBT2_SUPPORT
		pTxt = &item_t2_signal_only_label;
		OSD_SetTextFieldStrPoint(pTxt,display_strs[1]);
		OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"T2 Signal Only");
		#ifdef DVBT2_LITE_SUPPORT
			pTxt = &item_t2_lite_support_label;
			OSD_SetMultiselSel(&item_t2_lite_support_sel, sys_data->t2_lite_support);
			OSD_SetTextFieldStrPoint(pTxt,display_strs[2]);
			OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"T2-Lite Support");
		#endif
	#endif		
        is_auto_scan=TRUE;//modify for channel scan mode show the title auto scan on T tuner 2011 11 25
		wincom_open_title(pObj,title_id,0);
       // OSD_SetMultiselSel(&item_country_sel, sys_data->country);
       
		#if( SYS_FTA_ONLY_OFF ==  SYS_FUNC_OFF)  // Gavim@20140915
		remove_menu_item((CONTAINER *)pObj,(POBJECT_HEAD)&item_fta_only, CON_H + CON_GAP);
		#endif
     
		break;
	case EVN_POST_OPEN:
		break;
	case EVN_PRE_CLOSE:
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;		
 		sys_data->country = OSD_GetMultiselSel(&item_country_sel);
	#ifdef DVBT2_SUPPORT
		if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev, HLD_DEV_TYPE_NIM))
		{
			searchT2SignalOnly = OSD_GetMultiselSel(&item_t2_signal_only_sel);
			nim_io_control(nim_dev, NIM_DRIVER_SEARCH_T2_SIGNAL_ONLY, searchT2SignalOnly);
			#ifdef DVBT2_LITE_SUPPORT
		 		sys_data->t2_lite_support= OSD_GetMultiselSel(&item_t2_lite_support_sel);
				nim_io_control(nim_dev, NIM_DRIVER_T2_LITE_ENABLE, (UINT32)(sys_data->t2_lite_support));
			#endif
		}
	#endif
  		sys_data_save(1);
		break;
	case EVN_POST_CLOSE:
		//sdt_monitor_off();
		break;			
	}
	return ret;        
}

void win_autoscan_set_isdbt_param(BOOL isdbt_val)
{
	current_frontend_is_isdbt=isdbt_val;
}
